<?php
/////////////////////////////////////////////////////////////////////////////
// FleaPHP Framework
//
// Copyright (c) 2005 - 2008 QeeYuan China Inc. (http://www.qeeyuan.com)
//
// 许可协议，请查看源代码中附带的 LICENSE.txt 文件，
// 或者访问 http://www.fleaphp.org/ 获得详细信息。
/////////////////////////////////////////////////////////////////////////////

/**
 * 定义 Controller_BoArticles 类
 *
 * @copyright Copyright (c) 2005 - 2008 QeeYuan China Inc. (http://www.qeeyuan.com)
 * @author 起源科技 (www.qeeyuan.com)
 * @package Example
 * @subpackage SHOP
 * @version $Id: BoArticles.php 972 2007-10-09 20:56:54Z qeeyuan $
 */

// {{{ includes
FLEA::loadClass('Controller_BoBase');
// }}}

/**
 * Controller_BoArticles 提供了操作产品信息的后台界面功能
 *
 * @package Example
 * @subpackage SHOP
 * @author 起源科技 (www.qeeyuan.com)
 * @version 1.0
 */
class Controller_BoArticles extends Controller_BoBase
{
    /**
     * 操作产品分类的对象
     *
     * @var Model_ArticleClasses
     */
    var $_modelClasses;
    
    /**
     * 操作地区分类的对象
     *
     * @var Model_CitysClasses
     */
    var $_modelcityClasses;

    /**
     * @var Model_Articles
     */
    var $_modelArticles;
    
    /**
     * @var access
     */
    var $access;
    

    /**
     * 构造函数
     *
     * @return Controller_BoArticles
     */
    function Controller_BoArticles() {
        parent::Controller_BoBase();
        $this->access = isset($_GET['catid']) ? (int)$_GET['catid'] : null;
        //用户角色权限控制
        if(!$this->checkDispatcher($this->access)){
            redirect(url('BoDashboard','ErrorIn'));
        }
        $this->_modelClasses =& FLEA::getSingleton('Model_Categories');
        $this->_modelcityClasses =& FLEA::getSingleton('Model_CityClasses');
        $this->_modelArticles =& FLEA::getSingleton('Model_Articles');
    }

    /**
     * 显示文章列表
     */
    function actionIndex() {
		$catid = isset($_GET['catid']) ? (int)$_GET['catid'] : null;
		if ($catid) {
            $field = array('class_id' => $catid);
        } else {
            $field = null;
        }
        $page = isset($_GET['page']) ? (int)$_GET['page'] : 0;
        FLEA::loadClass('FLEA_Helper_Pager');
        $table =& $this->_modelArticles->getTable();
        $pager =& new FLEA_Helper_Pager($table, $page, 20, $field);
        $pk = $table->primaryKey;
		$viewdata = array();

        
        $rowset = $pager->findAll("*",$field);

        $this->_setBack();
//        var_dump($this->_url('Index',array('catid' => $catid, 'access' => $this->access)));
        include(TPL_DIR . '/BoArticlesList.php');
    }

    /**
     * 添加文章
     */
    function actionCreate() {
        $table =& $this->_modelArticles->getTable();
        $article = $this->_prepareData($table->meta);
        $this->_editArticle($article);
    }

    /**
     * 修改文章
     */
    function actionEdit() {
        $article = $this->_modelArticles->getArticle($_GET['id']);
        $this->_editArticle($article);
    }

    /**
     * 显示文章信息编辑页面
     *
     * @param array $article
     * @param string $errorMessage
     */
    function _editArticle($article, $errorMessage = '') {
        $table =& $this->_modelArticles->getTable();
        $pk = $table->primaryKey;
        $catid = isset($_GET['catid']) ? (int)$_GET['catid'] : null;
        include(TPL_DIR . '/BoArticleEdit.php');
    }

    /**
     * 保存文章信息
     */
    function actionSave() {
        __TRY();
//		$dispatcher =& $this->_getDispatcher();
        $user = $this->getUser();
		$_POST['created'] = time(); 
		$_POST['author'] = $user['ID'];
		$_POST['content'] = $_POST['spaw1'];

        $this->_modelArticles->saveArticle($_POST);
        $ex = __CATCH();
        if (__IS_EXCEPTION($ex)) {
            return $this->_editArticle($_POST, $ex->getMessage());
        }

        $catid = isset($_GET['catid']) ? (int)$_GET['catid'] : null;
        if($catid == 8){
        	$catid = $_POST['class_id'];
        }

        redirect($this->_url('Index',array('catid' => $catid, 'access' => $this->access)));
    }

    /**
     * 删除文章
     */
    function actionRemove() {
        $this->_modelArticles->removeArticle($_GET['id']);
        $this->_goBack();
    }

    /**
     * 显示产品的图片管理器
     */
    function actionPicman($errorMessage = null) {
        $article = $this->_modelArticles->getArticle($_GET['id'], true);
        if (!$article) { $this->_goBack(); }
        $table =& $this->_modelArticles->getTable();
        $pk = $table->primaryKey;
        include(TPL_DIR . '/BoArticlePicman.php');
    }

    /**
     * 上传缩略图
     */
    function actionUploadThumb() {
        $this->_uploadPicture('thumb');
    }

    /**
     * 处理图片上传
     */
    function _uploadPicture($pictureType) {
        $articleId = (int)$_GET['id'];
        do {
            $uploader =& FLEA::getSingleton('FLEA_Helper_FileUploader');
            /* @var $uploader FLEA_Helper_FileUploader */

            // 检查上传文件是否存在
            if (!$uploader->isFileExist('postfile')) {
                $errorMessage = _T('ui_p_upload_failed');
                break;
            }

            // 检查文件扩展名是否是允许上传的类型
            $file =& $uploader->getFile('postfile');
            if (!$file->check(FLEA::getAppInf('thumbFileExts'))) {
                $errorMessage =_T('ui_p_invalid_filetype');
                break;
            }

            // 为指定产品设置缩略图
            __TRY();
            if ($pictureType == 'thumb') {
                $this->_modelArticles->uploadThumb($articleId, $file);
            } else {
                $this->_modelArticles->uploadPhoto($articleId, $file);
            }
            $ex = __CATCH();
            if (__IS_EXCEPTION($ex)) {
                $errorMessage = $ex->getMessage();
                break;
            }

            redirect($this->_url('picman', array('id' => $articleId)));
        } while (false);

        $this->actionPicman($errorMessage);
    }

    /**
     * 上传缩略图
     */
    function actionUploadPhoto() {
        $this->_uploadPicture('photo');
    }

    /**
     * 删除指定图片
     */
    function actionRemovePhoto() {
        $this->_modelArticles->removePhoto($_GET['id'], $_GET['photo_id']);
        redirect($this->_url('picman', array('id' => (int)$_GET['id'])));
    }
}
